
//EOF
/*
	CargoEnhancement.cpp
	(c)2008 Palestar Inc, Jeff Everett
*/


#include "Debug/Assert.h"
#include "Standard/Limits.h"
#include "NounPlanet.h"
#include "VerbLoad.h"
#include "NounShip.h"
#include "CargoEnhancement.h"
#include "StructurePort.h"
#include "ShipPlatform.h"
#include "Game/NounEnhancement.h"
#include "GameContext.h"

//----------------------------------------------------------------------------
const dword CARGO_ENHANCEMENT_LIFE =  300; // 5 minutes
//----------------------------------------------------------------------------

IMPLEMENT_FACTORY( CargoEnhancement, NounCargo );

BEGIN_PROPERTY_LIST( CargoEnhancement, NounCargo )
	ADD_TRANSMIT_UPDATE_PROPERTY( m_enhancementClassKey );
END_PROPERTY_LIST();

CargoEnhancement::CargoEnhancement() : m_nRemovalTime( 0.0f ),m_enhancementClassKey(qword(0)),m_pEnhancement(NULL)
{}

CargoEnhancement::CargoEnhancement( ClassKey a_classKey ) : m_nRemovalTime( 0.0f ),m_pEnhancement(NULL)
{
	m_enhancementClassKey = a_classKey;
	// set the name
	setQuantity(1);
	createEnhancement();
}

//----------------------------------------------------------------------------

bool CargoEnhancement::read( InStream & input )
{
	if (! NounCargo::read( input ) )
		return false;
	return true;
}

//----------------------------------------------------------------------------

void CargoEnhancement::onAttached()
{
	NounCargo::onAttached();
	createEnhancement();
	updateName();	
}

void CargoEnhancement::onDetaching()
{
	NounCargo::onDetaching();
	m_pEnhancement = NULL; 
}

//----------------------------------------------------------------------------



void CargoEnhancement::initialize()
{
	m_nRemovalTime = 0.0f;
	NounCargo::initialize();
	// update name after init so that init doesnt reset the name
	createEnhancement();
	updateName();	
}

void CargoEnhancement::simulate( dword nTick )
{
	// NounCargo::simulate( dword nTick ) will remove the cargo object after timer expired
	float fElapsed = TICK_DURATION_S * (nTick - tick());

	m_nRemovalTime += fElapsed;

	// if we are not on a ship or a structureport
	if ( !WidgetCast<NounShip>( parent() ) && !WidgetCast<StructurePort>( parent() ))
	{
		if(m_nRemovalTime > CARGO_ENHANCEMENT_LIFE)
		{
			setDetach();
		}
	}
	NounCargo::simulate( nTick );
}

//----------------------------------------------------------------------------
CharString CargoEnhancement::getInfo()
{
	if(m_pEnhancement)
		return m_pEnhancement->GetDescription();
	else 
		return CharString("");
}

void CargoEnhancement::createEnhancement()
{
	// if its already created...dont
	if(m_pEnhancement)
		return;

	Factory * pFactory = Factory::findFactory( m_enhancementClassKey);
	if ( pFactory != NULL )
	{
		//create the enhancement 
		m_pEnhancement = WidgetCast<NounEnhancement>( pFactory->createWidget() );
	}
}

//----------------------------------------------------------------------------
void CargoEnhancement::updateName()
{
	if(m_pEnhancement)
		setName(CharString().format("%s",m_pEnhancement->GetName()));
}

CharString CargoEnhancement::status() const
{
	if(m_pEnhancement)
		return m_pEnhancement->status();
	else
		return NounGame::status();
}

float CargoEnhancement::baseSignature() const
{
	return 20.0f;
}


//----------------------------------------------------------------------------
//EOF
